--=======================================
--[[ 本地函数 ]]
--=======================================
-- 移除预制物(预制物,数量)
local function removeItem(item, num)
    if item.components.stackable then
        item.components.stackable:Get(num):Remove()
    else
        item:Remove()
    end
end

--[[
	tags所有标签: fruit-水果度, monster-怪物度, sweetener-甜度, veggie-菜度, meat-肉度, frozen-冰度, fish-鱼度, egg-蛋度
	decoration-装饰度-蝴蝶翅膀, fat-油脂度-黄油, dairy-奶度, inedible-不可食用度, seed-种子-桦栗果, magic-魔法度-噩梦燃料
    ]]
local foods =
{
    -- 猪猪晗
    sw_piggycorolla =
    {
        test = function(cooker, names, tags)
            return ((names.sw_piggymimosa or 0) + (names.sw_piggymimosa_cooked or 0)) >= 2 and
                (tags.meat and tags.meat >= 1) and names.refined_dust                                          -- 配方
        end,
        priority = 120,                                                                                        -- 优先级
        weight = 1,                                                                                            -- 权重
        foodtype = FOODTYPE.GOODIES,                                                                           -- 蔬菜:VEGGIE 肉:MEAT 好东西:GOODIES
        secondaryfoodtype = FOODTYPE.ROUGHAGE,                                                                 -- 第二食物类型
        health = 15,                                                                                           -- 生命值
        hunger = 15,                                                                                           -- 饥饿
        perishtime = nil,                                                                                      -- 腐烂时间
        sanity = 25,                                                                                           -- 精神
        oneat_desc = STRINGS.UI.COOKBOOK.SW_PIGGYCOROLLA,                                                      -- 食谱书中的描述
        cooktime = 1.5,                                                                                        -- 烹饪时间:1对应20s
        tags = { "sw_weavable" },
        floater = { "med", nil, 0.5 },                                                                         -- 漂浮在水面的参数
        card_def = { ingredients = { { "sw_piggymimosa", 2 }, { "monstermeat", 1 }, { "refined_dust", 1 } } }, -- 食谱卡
        prefabs = { "sw_buff_piggycorolla" },                                                                  -- 所需要引用的预制物名
        oneatenfn = function(inst, eater)                                                                      -- 食用时会触发的特殊效果
            if eater.components.combat ~= nil then
                eater:AddDebuff("sw_buff_piggycorolla", "sw_buff_piggycorolla")
            end
        end,
        master_postinit = function(inst)
            inst:AddComponent("sw_weavable")
            inst.components.sw_weavable:SetWeaveFn(function(target, invobject, doer)
                -- 在这里定义编织的具体逻辑
                local new_food = SpawnPrefab("sw_chasing_light_shadow")
                local x, y, z = target.Transform:GetWorldPosition()
                new_food.Transform:SetPosition(x, y, z)

                local owner = target.components.inventoryitem ~= nil and target.components.inventoryitem.owner or nil
                local holder = owner ~= nil and (owner.components.inventory or owner.components.container) or nil
                if holder ~= nil then
                    removeItem(target, 1)
                    holder:GiveItem(new_food)
                else
                    removeItem(target, 1)
                end

                if doer ~= nil and doer.components.cookbookupdater then
                    doer.components.cookbookupdater:LearnRecipe("sw_chasing_light_shadow", { "sw_woven", "sw_woven", "sw_piggycorolla", "sw_piggycorolla" })
                end
            end)
        end,
    },
    -- 夜莓小蛋糕
    sw_night_berry_cake =
    {
        test = function(cooker, names, tags)
            return ((names.ancientfruit_nightvision or 0) + (names.ancientfruit_nightvision_cooked or 0)) >= 2 and
                (tags.egg and tags.egg >= 1) and tags.sweetener
        end,
        priority = 90,
        weight = 1,
        foodtype = FOODTYPE.GOODIES,
        health = 0,
        hunger = 20,
        perishtime = TUNING.PERISH_SUPERFAST,
        sanity = 5,
        oneat_desc = STRINGS.UI.COOKBOOK.SW_NIGHT_BERRY_CAKE,
        cooktime = 1.25,
        floater = { "med", nil, 0.5 },
        card_def = { ingredients = { { "ancientfruit_nightvision", 3 }, { "piggymimosa", 1 } } },
        prefabs = { "sw_buff_night_berry_cake" },
        oneatenfn = function(inst, eater)
            eater:AddDebuff("sw_buff_night_berry_cake", "sw_buff_night_berry_cake")
        end,
    },
    -- 追光逐影
    sw_chasing_light_shadow =
    {
        test = function(cooker, names, tags)
            return false
        end,
        priority = 90,
        weight = 1,
        foodtype = FOODTYPE.GOODIES,
        health = 15,
        hunger = 15,
        perishtime = nil,
        sanity = 25,
        oneat_desc = STRINGS.UI.COOKBOOK.SW_CHASING_LIGHT_SHADOW,
        cooktime = 1.25,
        floater = { "med", nil, 0.5 },
        prefabs = { "sw_buff_piggycorolla", "sw_buff_chasing_light_shadow" },
        overridebuild = "sw_chasing_light_shadow",
        oneatenfn = function(inst, eater)
            eater:AddDebuff("sw_buff_piggycorolla", "sw_buff_piggycorolla")
            eater:AddDebuff("sw_buff_chasing_light_shadow", "sw_buff_chasing_light_shadow")
        end,
    },
}

for k, v in pairs(foods) do
    v.name = k
    v.basename = k
    v.weight = v.weight or 1
    v.priority = v.priority or 0
    if v.overridebuild == nil then
        v.overridebuild = "sw_foods"
    end
    v.mod = "sw"
    v.cookbook_category = "cookpot"

    -- 烹饪指南里的料理贴图
    if v.cookbook_atlas == nil then
        v.cookbook_atlas = "images/cookbook_images/cookbook_" .. k .. ".xml"
    end
    if v.cookbook_tex == nil then
        v.cookbook_tex = "" .. k .. ".tex"
    end
end

return foods
